#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;
using System.Collections;

#endregion

namespace Library
{
    public partial class FormBookStockAdjustment : Form, IFindBook
    {
        BookTransaction m_BookBookTransaction;
        User m_currnUser;
        BookTransactionService m_BookTransactionService = new BookTransactionService();
        BookService m_BookService = new BookService();
        UserControlBookStockAdjustment m_UserControlBookStockAdjustment;
        Book m_book = null;
        bool m_apply = false;

        public FormBookStockAdjustment(BookTransaction BookTransaction, User currnBookCategory, UserControlBookStockAdjustment BookCategoryControlBookCategory)
        {
            InitializeComponent();
            labelTitle.Text = "Book Stock Adjustment";
            Text = "Book Stock Adjustment";
            m_BookBookTransaction = BookTransaction;
            m_currnUser = currnBookCategory;
            m_UserControlBookStockAdjustment = BookCategoryControlBookCategory;
            initComboBox();
            loadBookCategory();
            initEnterKeyDown(this.Controls);
        }

        private void initComboBox()
        {
            IList trType = new ArrayList();
            trType.Add(TransactionType.Purchase);
            trType.Add(TransactionType.Donation);
            trType.Add(TransactionType.Lost);
            trType.Add(TransactionType.Broken);

            comboBoxTRansactionType.DataSource = trType;
        }

        private void initEnterKeyDown(Control.ControlCollection controls)
        {
            foreach (Control c in controls)
            {
                if (c is TextBox)
                {
                    TextBox txtB = (TextBox)c;
                    txtB.EnterKeyDown += new KeyEventHandler(txtB_EnterKeyDown);
                }
                if (c is CheckBox)
                {
                    CheckBox txtB = (CheckBox)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                if (c is ComboBox)
                {
                    ComboBox txtB = (ComboBox)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                if (c is DateTimePicker)
                {
                    DateTimePicker txtB = (DateTimePicker)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                if (c is NumericUpDown)
                {
                    NumericUpDown txtB = (NumericUpDown)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                initEnterKeyDown(c.Controls);
            }
        }

        void txtB_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                Control ctrl = (Control)objSender;
                SelectNextControl(ctrl, true, true, true, true);
            }
        }

        void txtB_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            Control ctrl = (Control)objSender;
            SelectNextControl(ctrl, true, true, true, true);
        }

        private void loadBookCategory()
        {
            if (m_BookBookTransaction.Id > 0)
            {
                m_book = m_BookService.GetBookById(m_BookBookTransaction.BookId);
                textBoxBookCode.Text = m_book.Code;
            }
            textBoxRemark.Text = m_BookBookTransaction.Remark;
            comboBoxTRansactionType.SelectedItem = m_BookBookTransaction.TransactionType;
            dateTimePickerTrDate.Value = m_BookBookTransaction.TransactionDate;
            numericUpQuantity.Value = m_BookBookTransaction.Quantity;

            buttonFindBook.Visible = m_BookBookTransaction.Id == 0;
            textBoxBookCode.Enabled = m_BookBookTransaction.Id == 0;
            buttonApply.Visible = m_BookBookTransaction.Id != 0;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (isValid())
            {
                UpdateEntity();
                try
                {
                    if (m_BookBookTransaction.Id == 0)
                    {
                        m_BookTransactionService.InsertNewBookTransaction(m_BookBookTransaction);
                        m_UserControlBookStockAdjustment.AddNewData(m_BookBookTransaction);
                    }
                    else
                    {
                        m_BookTransactionService.UpdateBookTransaction(m_BookBookTransaction);
                        m_UserControlBookStockAdjustment.UpdateData(m_BookBookTransaction);
                    }
                    MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, new EventHandler(CloseForm));
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }

            }
        }
        void CloseForm(object sender, EventArgs e)
        {
            if (!m_apply)
                Close();
            m_apply = false;
        }

        private void UpdateEntity()
        {
            m_BookBookTransaction.Name = m_book.Code + " - " + m_book.Name;
            m_BookBookTransaction.Remark = textBoxRemark.Text;
            m_BookBookTransaction.CreatedUserId = m_currnUser.Id;
            m_BookBookTransaction.ModifiedUserId = m_currnUser.Id;
            m_BookBookTransaction.BookId = m_book.Id;
            m_BookBookTransaction.TransactionDate = dateTimePickerTrDate.Value;
            m_BookBookTransaction.TransactionType = (TransactionType)comboBoxTRansactionType.SelectedValue;
            m_BookBookTransaction.Quantity = numericUpQuantity.Value;
        }

        private bool isValid()
        {
            clearError();
            bool validBookcode = textBoxBookCode.Text != "";
            bool validQty = numericUpQuantity.Value != 0;
            if (validBookcode)
            {
                m_book = m_BookService.GetBookByBookCode(textBoxBookCode.Text);
                validBookcode = m_book != null;
            }
            if (!validBookcode) errorProvider.SetError(textBoxBookCode, "Fill Book Code / Invalid Book");
            if (!validQty) errorProvider.SetError(numericUpQuantity, "Fill Qty");
            return validBookcode && validQty;
        }

        private void clearError()
        {
            errorProvider.SetError(textBoxBookCode, "");
            errorProvider.SetError(numericUpQuantity, "");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            m_apply = true;
            buttonSave_Click(sender, e);
        }

        private void buttonFindBook_Click(object sender, EventArgs e)
        {
            FormFindBook form = new FormFindBook(this);
            form.ShowDialog();
        }
        public void SetBook(Book book)
        {
            m_book = book;
            textBoxBookCode.Text = book.Code;
            textBoxBookCode.Focus();
            textBoxBookCode.SelectAll();
        }

    }
}